#include "../DataStructures/allocator.h"

typedef struct _subs {
    int num;
    char name[20];
} subs;

void alloc_test() {
    int k, cases, i = 0;
    SAllocator a;
    subs * s[20];

#ifndef ONLINE_JUDGE
    freopen("alloc_test.in", "r", stdin);
#endif

    scanf("%d", &cases);
    while (cases--) {
        alloc_init(&a, 2, sizeof (subs));
        scanf("%d", &k);

        for (i = 0; i < k; i++) {
            s[i] = alloc_get(&a);
            if (s[i] == NULL) {
                printf("no more free memory [%d]\n", i);
                break;
            }
            scanf("%d%s", &(s[i]->num), s[i]->name);
        }

        k = i;
        for (i = 0; i < k; i++)
            printf("%d %s\n", s[i]->num, s[i]->name);

        alloc_free(&a, s[0]);
        s[0] = NULL;
        
        printf("Avail blocks [%d]\n", alloc_avail(&a));

        s[2] = alloc_get(&a);
        s[2]->num = 232;
        strcpy(s[2]->name, "from freedpool");

        printf("Avail blocks [%d] [%p]\n", alloc_avail(&a), s[2]);
        printf("%d %s\n", s[2]->num, s[2]->name);

        alloc_free(&a, s[1]);
        s[1] = NULL;
        alloc_free(&a, s[2]);
        s[2] = NULL;
        
        s[10] = alloc_get(&a);
        if (s[10] != 0) {
            s[10]->num = 233;
            strcpy(s[10]->name, "same memory");

            printf("Avail blocks [%d] [%p]\n", alloc_avail(&a), s[10]);
            printf("%d %s\n", s[10]->num, s[10]->name);
        }
    }

    return;
}
